<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

/**
 * 后台公共文件
 * 主要定义后台公共函数库
 */

/**
 * 获取扩展模型对象
 * @param  integer $model_id 模型编号
 * @return object         模型对象
 */
function logic($model_id){
    $name  = parse_name(get_document_model($model_id, 'name'), 1);
    $class = is_file(MODULE_PATH . 'Logic/' . $name . 'Logic' . EXT) ? $name : 'Base';
    $class = MODULE_NAME . '\\Logic\\' . $class . 'Logic';
    return new $class($name);
}

/* 解析列表定义规则*/

function get_list_field($data, $grid){

    // 获取当前字段数据
    foreach($grid['field'] as $field){
        $array  =   explode('|',$field);
        $temp  =    $data[$array[0]];
        // 函数支持
        if(isset($array[1])){
            $temp = call_user_func($array[1], $temp);
        }
        $data2[$array[0]]    =   $temp;
    }
    if(!empty($grid['format'])){
        $value  =   preg_replace_callback('/\[([a-z_]+)\]/', function($match) use($data2){return $data2[$match[1]];}, $grid['format']);
    }else{
        $value  =   implode(' ',$data2);
    }

    // 链接支持
    if('title' == $grid['field'][0] && '目录' == $data['type'] ){
        // 目录类型自动设置子文档列表链接
        $grid['href']   =   '[LIST]';
    }
    if(!empty($grid['href'])){
        $links  =   explode(',',$grid['href']);
        foreach($links as $link){
            $array  =   explode('|',$link);
            $href   =   $array[0];
            if(preg_match('/^\[([a-z_]+)\]$/',$href,$matches)){
                $val[]  =   $data2[$matches[1]];
            }else{
                $show   =   isset($array[1])?$array[1]:$value;
                // 替换系统特殊字符串
                $href   =   str_replace(
                    array('[DELETE]','[EDIT]','[LIST]'),
                    array('setstatus?status=-1&ids=[id]',
                    'edit?id=[id]&model=[model_id]&cate_id=[category_id]',
                    'index?pid=[id]&model=[model_id]&cate_id=[category_id]'),
                    $href);

                // 替换数据变量
                $href   =   preg_replace_callback('/\[([a-z_]+)\]/', function($match) use($data){return $data[$match[1]];}, $href);

                $val[]  =   '<a href="'.U($href).'">'.$show.'</a>';
            }
        }
        $value  =   implode(' ',$val);
    }
    return $value;
}

/* 解析插件数据列表定义规则*/

function get_addonlist_field($data, $grid,$addon){
    // 获取当前字段数据
    foreach($grid['field'] as $field){
        $array  =   explode('|',$field);
        $temp  =    $data[$array[0]];
        // 函数支持
        if(isset($array[1])){
            $temp = call_user_func($array[1], $temp);
        }
        $data2[$array[0]]    =   $temp;
    }
    if(!empty($grid['format'])){
        $value  =   preg_replace_callback('/\[([a-z_]+)\]/', function($match) use($data2){return $data2[$match[1]];}, $grid['format']);
    }else{
        $value  =   implode(' ',$data2);
    }

    // 链接支持
    if(!empty($grid['href'])){
        $links  =   explode(',',$grid['href']);
        foreach($links as $link){
            $array  =   explode('|',$link);
            $href   =   $array[0];
            if(preg_match('/^\[([a-z_]+)\]$/',$href,$matches)){
                $val[]  =   $data2[$matches[1]];
            }else{
                $show   =   isset($array[1])?$array[1]:$value;
                // 替换系统特殊字符串
                $href   =   str_replace(
                    array('[DELETE]','[EDIT]','[ADDON]'),
                    array('del?ids=[id]&name=[ADDON]','edit?id=[id]&name=[ADDON]',$addon),
                    $href);

                // 替换数据变量
                $href   =   preg_replace_callback('/\[([a-z_]+)\]/', function($match) use($data){return $data[$match[1]];}, $href);

                $val[]  =   '<a href="'.U($href).'">'.$show.'</a>';
            }
        }
        $value  =   implode(' ',$val);
    }
    return $value;
}

// 获取模型名称
function get_model_by_id($id){
    return $model = M('Model')->getFieldById($id,'title');
}

// 获取属性类型信息
function get_attribute_type($type=''){
    // TODO 可以加入系统配置
    static $_type = array(
        'num'       =>  array('数字','int(10) UNSIGNED NOT NULL'),
        'string'    =>  array('字符串','varchar(255) NOT NULL'),
        'textarea'  =>  array('文本框','text NOT NULL'),
        'date'      =>  array('日期','int(10) NOT NULL'),
        'datetime'  =>  array('时间','int(10) NOT NULL'),
        'password'  =>  array('密码','varchar(255) NOT NULL'),
        'bool'      =>  array('布尔','tinyint(2) NOT NULL'),
        'select'    =>  array('枚举','char(50) NOT NULL'),
        'radio'     =>  array('单选','char(10) NOT NULL'),
        'checkbox'  =>  array('多选','varchar(100) NOT NULL'),
        'editor'    =>  array('编辑器','text NOT NULL'),
        'picture'   =>  array('上传图片','int(10) UNSIGNED NOT NULL'),
        'file'      =>  array('上传附件','int(10) UNSIGNED NOT NULL'),
        'files'      =>  array('上传多附件','varchar(255) NOT NULL'),
        'category'  =>  array('三级联动','varchar(255) NOT NULL'),
        'cities'  =>  array('省市区三级联动','varchar(255) NOT NULL'),
        'attribute'  =>  array('自定义属性','text NOT NULL'),
        'multicheckbox'  =>  array('多选联动','varchar(255) NOT NULL'),
        'uploadpics' => array('批量上传图片','varchar(255) NOT NULL'), //批量上传图片项
    );
    return $type?$_type[$type][0]:$_type;
}

/**
 * 获取对应状态的文字信息
 * @param int $status
 * @return string 状态文字 ，false 未获取到
 * @author huajie <banhuajie@163.com>
 */
function get_status_title($status = null){
    if(!isset($status)){
        return false;
    }
    switch ($status){
        case -1 : return    '已删除';   break;
        case 0  : return    '禁用';     break;
        case 1  : return    '正常';     break;
        case 2  : return    '待审核';   break;
        default : return    false;      break;
    }
}

// 获取数据的状态操作
function show_status_op($status) {
    switch ($status){
        case 0  : return    '启用';     break;
        case 1  : return    '禁用';     break;
        case 2  : return    '审核';       break;
        default : return    false;      break;
    }
}

/**
 * 获取文档的类型文字
 * @param string $type
 * @return string 状态文字 ，false 未获取到
 * @author huajie <banhuajie@163.com>
 */
function get_document_type($type = null){
    if(!isset($type)){
        return false;
    }
    switch ($type){
        case 1  : return    '目录'; break;
        case 2  : return    '主题'; break;
        case 3  : return    '段落'; break;
        default : return    false;  break;
    }
}

/**
 * 获取配置的类型
 * @param string $type 配置类型
 * @return string
 */
function get_config_type($type=0){
    $list = C('CONFIG_TYPE_LIST');
    return $list[$type];
}

/**
 * 获取配置的分组
 * @param string $group 配置分组
 * @return string
 */
function get_config_group($group=0){
    $list = C('CONFIG_GROUP_LIST');
    return $group?$list[$group]:'';
}

/**
 * select返回的数组进行整数映射转换
 *
 * @param array $map  映射关系二维数组  array(
 *                                          '字段名1'=>array(映射关系数组),
 *                                          '字段名2'=>array(映射关系数组),
 *                                           ......
 *                                       )
 * @author 朱亚杰 <zhuyajie@topthink.net>
 * @return array
 *
 *  array(
 *      array('id'=>1,'title'=>'标题','status'=>'1','status_text'=>'正常')
 *      ....
 *  )
 *
 */
function int_to_string(&$data,$map=array('status'=>array(1=>'正常',-1=>'删除',0=>'禁用',2=>'未审核',3=>'草稿'))) {
    if($data === false || $data === null ){
        return $data;
    }
    $data = (array)$data;
    foreach ($data as $key => $row){
        foreach ($map as $col=>$pair){
            if(isset($row[$col]) && isset($pair[$row[$col]])){
                $data[$key][$col.'_text'] = $pair[$row[$col]];
            }
        }
    }
    return $data;
}

/**
 * 动态扩展左侧菜单,base.html里用到
 * @author 朱亚杰 <zhuyajie@topthink.net>
 */
function extra_menu($extra_menu,&$base_menu){
    foreach ($extra_menu as $key=>$group){
        if( isset($base_menu['child'][$key]) ){
            $base_menu['child'][$key] = array_merge( $base_menu['child'][$key], $group);
        }else{
            $base_menu['child'][$key] = $group;
        }
    }
}

/**
 * 获取参数的所有父级分类
 * @param int $cid 分类id
 * @return array 参数分类和父类的信息集合
 * @author huajie <banhuajie@163.com>
 */
function get_parent_category($cid){
    if(empty($cid)){
        return false;
    }
    $cates  =   M('Category')->where(array('status'=>1))->field('id,title,pid')->order('sort')->select();
    $child  =   get_category($cid); //获取参数分类的信息
    $pid    =   $child['pid'];
    $temp   =   array();
    $res[]  =   $child;
    while(true){
        foreach ($cates as $key=>$cate){
            if($cate['id'] == $pid){
                $pid = $cate['pid'];
                array_unshift($res, $cate); //将父分类插入到数组第一个元素前
            }
        }
        if($pid == 0){
            break;
        }
    }
    return $res;
}

/**
 * 检测验证码
 * @param  integer $id 验证码ID
 * @return boolean     检测结果
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function check_verify($code, $id = 1){
    $verify = new \Think\Verify();
    return $verify->check($code, $id);
}

/**
 * 获取当前分类的文档类型
 * @param int $id
 * @return array 文档类型数组
 * @author huajie <banhuajie@163.com>
 */
function get_type_bycate($id = null){
    if(empty($id)){
        return false;
    }
    $type_list  =   C('DOCUMENT_MODEL_TYPE');
    $model_type =   M('Category')->getFieldById($id, 'type');
    $model_type =   explode(',', $model_type);
    foreach ($type_list as $key=>$value){
        if(!in_array($key, $model_type)){
            unset($type_list[$key]);
        }
    }
    return $type_list;
}

/**
 * 获取当前文档的分类
 * @param int $id
 * @return array 文档类型数组
 * @author huajie <banhuajie@163.com>
 */
function get_cate($cate_id = null){
    if(empty($cate_id)){
        return false;
    }
    $cate   =   M('Category')->where('id='.$cate_id)->getField('title');
    return $cate;
}

 // 分析枚举类型配置值 格式 a:名称1,b:名称2
function parse_config_attr($string) {
    $array = preg_split('/[,;\r\n]+/', trim($string, ",;\r\n"));
    if(strpos($string,':')){
        $value  =   array();
        foreach ($array as $val) {
            list($k, $v) = explode(':', $val);
            $value[$k]   = $v;
        }
    }else{
        $value  =   $array;
    }
    return $value;
}

// 获取子文档数目
function get_subdocument_count($id=0){
    return  M('Document')->where('pid='.$id)->count();
}



 // 分析枚举类型字段值 格式 a:名称1,b:名称2
 // 暂时和 parse_config_attr功能相同
 // 但请不要互相使用，后期会调整
function parse_field_attr($string) {
    if(0 === strpos($string,':')){
        // 采用函数定义
        return   eval('return '.substr($string,1).';');
    }elseif(0 === strpos($string,'[')){
        // 支持读取配置参数（必须是数组类型）
        return C(substr($string,1,-1));
    }
    
    $array = preg_split('/[,;\r\n]+/', trim($string, ",;\r\n"));
    if(strpos($string,':')){
        $value  =   array();
        foreach ($array as $val) {
            list($k, $v) = explode(':', $val);
            $value[$k]   = $v;
        }
    }else{
        $value  =   $array;
    }
    return $value;
}

/**
 * 获取行为数据
 * @param string $id 行为id
 * @param string $field 需要获取的字段
 * @author huajie <banhuajie@163.com>
 */
function get_action($id = null, $field = null){
    if(empty($id) && !is_numeric($id)){
        return false;
    }
    $list = S('action_list');
    if(empty($list[$id])){
        $map = array('status'=>array('gt', -1), 'id'=>$id);
        $list[$id] = M('Action')->where($map)->field(true)->find();
    }
    return empty($field) ? $list[$id] : $list[$id][$field];
}

/**
 * 根据条件字段获取数据
 * @param mixed $value 条件，可用常量或者数组
 * @param string $condition 条件字段
 * @param string $field 需要返回的字段，不传则返回整个数据
 * @author huajie <banhuajie@163.com>
 */
function get_document_field($value = null, $condition = 'id', $field = null){
    if(empty($value)){
        return false;
    }

    //拼接参数
    $map[$condition] = $value;
    $info = M('Model')->where($map);
    if(empty($field)){
        $info = $info->field(true)->find();
    }else{
        $info = $info->getField($field);
    }
    return $info;
}

/**
 * 获取行为类型
 * @param intger $type 类型
 * @param bool $all 是否返回全部类型
 * @author huajie <banhuajie@163.com>
 */
function get_action_type($type, $all = false){
    $list = array(
        1=>'系统',
        2=>'用户',
    );
    if($all){
        return $list;
    }
    return $list[$type];
}

/**
 * 列表展示单张图片
 * @param id
 * @param 
 * @author 
 */
function get_picture_url($id){
    if(!$id){
        return ;
    }
    $re_pic = M('picture')->find($id);
    $img = "<img src='".__ROOT__.$re_pic['path']."' width='80'>";
    return $img;
}

/**
 * 票务信息 相关展览
 * @param id
 * @param 
 * @author 
 */
function show_ticket_exhibition($id){
    if(!$id){
        return ;
    }
    $res = M('exhibition')->find($id);
    return $res['name'];
}

/**
 * 场馆 城市显示
 * @param id
 * @param 
 * @author 
 */
function show_city($id){
    if(!$id){
        return ;
    }
    $ids = explode(',',$id);
    // $ids = array_reverse($id);
    if(!$id[1]){
       return $id; 
    }
    $res = M('vc_citys')->find($ids[1]);

    return $res['name'];
}

/**
 * 展览 场馆显示
 * @param id
 * @param 
 * @author 
 */
function show_museum($id){
    if(!$id){
        return ;
    }
    $res = M('museum')->find($id);

    return $res['name'];
}

/**
 * 用户名显示
 * @param id
 * @param 
 * @author 
 */
function show_visitor_name($id){
    if(!$id){
        return ;
    }
    $res = M('visitor')->find($id);

    return $res['name'];
}

/**
 * 用户昵称
 * @param id
 * @param 
 * @author 
 */
function show_visitor_nickname($id){
    if(!$id){
        return ;
    }
    $res = M('visitor')->find($id);

    return $res['nickname'];
}

/**
 * 展览显示
 * @param ticket_id
 * @param 
 * @author 
 */
function show_exhibition_name($ticket_id){
    if(!$ticket_id){
        return ;
    }
    $res_ticket = M('ticket')->find($ticket_id);
    $res_exhibition = M('exhibition')->find($res_ticket['exhibition_id']);

    return $res_exhibition['name'];
}

/**
 * 兑换码转下单者姓名
 * @param ticket_order_id
 * @param 
 * @author 
 */
function order_to_name($ticket_order_id){
    if(!$ticket_order_id){
        return ;
    }
    $res_user = M('visitor')
    ->field('visitor.*,ticket_order.id as order_id,ticket_order.visitor_id')
    ->where(array('ticket_order.id'=>$ticket_order_id))
    ->join('ticket_order on ticket_order.visitor_id = visitor.id')
    ->find();

    return $res_user['name']?$res_user['name']:'无';
}

/**
 * 兑换码转下单者昵称
 * @param ticket_order_id
 * @param 
 * @author 
 */
function order_to_nickname($ticket_order_id){
    if(!$ticket_order_id){
        return ;
    }
    $res_user = M('visitor')
    ->field('visitor.*,ticket_order.id as order_id,ticket_order.visitor_id')
    ->where(array('ticket_order.id'=>$ticket_order_id))
    ->join('ticket_order on ticket_order.visitor_id = visitor.id')
    ->find();

    return $res_user['nickname']?$res_user['nickname']:'无';
}

/**
 * 兑换码转下单者姓名
 * @param ticket_order_id
 * @param 
 * @author 
 */
function order_to_price($ticket_order_id){
    if(!$ticket_order_id){
        return ;
    }
    $re = M('ticket_order')
    ->field('ticket_order.*,payment.price as total')
    ->where(array('ticket_order.id'=>$ticket_order_id))
    ->join('payment on payment.id = ticket_order.payment_num','left')
    ->find();
    return $re['total']==null?$re['price']*$re['amount']:$re['total'];
}

/**
 * 兑换码转展览名
 * @param ticket_order_id
 * @param 
 * @author 
 */
function order_to_ticketname($ticket_order_id){
    if(!$ticket_order_id){
        return ;
    }
    $res_ticket = M('ticket')
    ->field('ticket.*,ticket_order.id as order_id,ticket_order.ticket_id,exhibition.id as exhibitionid,exhibition.name as exhibitionname')
    ->where(array('ticket_order.id'=>$ticket_order_id))
    ->join('ticket_order on ticket_order.ticket_id = ticket.id','left')
    ->join('exhibition on exhibition.id = ticket.exhibition_id')
    ->find();

    $res = $res_ticket['exhibitionname'].$res_ticket['name'];

    return $res;
}

/**
 * 兑换码转下单者手机
 * @param ticket_order_id
 * @param 
 * @author 
 */
function order_to_phone($ticket_order_id){
    if(!$ticket_order_id){
        return ;
    }
    $res_user = M('visitor')
    ->field('visitor.*,ticket_order.id as order_id,ticket_order.visitor_id')
    ->where(array('ticket_order.id'=>$ticket_order_id))
    ->join('ticket_order on ticket_order.visitor_id = visitor.id','left')
    ->find();

    return $res_user['phone']?$res_user['phone']:'无';
}

/**
 * 兑换码转下订单号
 * @param ticket_order_id
 * @param 
 * @author 
 */
function order_to_ordernum($ticket_order_id){
    if(!$ticket_order_id){
        return ;
    }
    $re = M('ticket_order')
    ->where(array('ticket_order.id'=>$ticket_order_id))
    ->find();

    return $re['order_num'];
}

/**
 * 作品显示艺术家名字
 * @param 
 * @param 
 * @author 
 */
function work_to_artistname($artist_id){
    if(!$artist_id){
        return ;
    }
    $re = M('artist')->find($artist_id);

    return $re['name'];
}

/*
 * 票务订单转票名
 * @param ticket_id
 * @param 
 * @author 
 */
function ticketorder_to_ticketname($ticket_id){
    if(!$ticket_id){
        return ;
    }
    $res_ticket = M('ticket')
    ->find($ticket_id);

    return $res_ticket['name'];
}

/**
 * 父分类显示
 * @param 
 * @param 
 * @author 
 */
function parent_category($category_id){
    if($category_id==0){
        return '无';
    }
    $re = M('product_category')->find($category_id);
    return $re['name'];
}

/**
 * 商品分类显示
 * @param 
 * @param 
 * @author 
 */
function product_category($category_id){
    if(!$category_id){
        return ;
    }
    $re = M('product_category')->find($category_id);
    return $re['name'];
}

/**
 * 商品价格区间
 * @param 
 * @param 
 * @author 
 */
function product_price($product_id){
    if(!$product_id){
        return ;
    }
    $attr = M('product_attribution')
    ->where(array('product_id'=>$product_id,'is_deleted'=>0))
    ->order('price asc')
    ->select();
    $len = count($attr);
    if($len==1){
        $price = '￥'.$attr[0]['price'];
    }
    if($len>1){
        $price = '￥'.$attr[0]['price'].'-￥'.$attr[$len-1]['price'];
    }
    return $price;
}

/**
 * 商品库存
 * @param 
 * @param 
 * @author 
 */
function product_stock($product_id){
    if(!$product_id){
        return ;
    }
    $attr = M('product_attribution')
    ->where(array('product_id'=>$product_id,'is_deleted'=>0))
    ->order('price asc')
    ->select();
    $sum = 0;
    foreach ($attr as $key => $value) {
        # code...
        $sum = $sum+$value['stock'];
    }
    return $sum;
}

/**
 * 购物车显示商品
 * @param 
 * @param 
 * @author 
 */
function cart_to_product($cart_ids){
    if(strlen($cart_ids)>0){
      $ids = explode(',',$cart_ids);
      $products = array();
      foreach ($ids as $key => $value) {
        $product = M('cart')
        ->field('cart.*,picture.path as product_cover,product.name as product_name,product.name_en as product_name_en,product_attribution.name as attr_name')
        ->where(array('cart.id'=>$value))
        ->join('product on product.id = cart.product_id')
        ->join('product_attribution on product_attribution.id = cart.attribution_id')
        ->join('picture on picture.id = product.cover','left')
        ->find();
        array_push($products,$product);
      }
      // var_dump($products);
      foreach ($products as $key => $value) {
          # code...
        if($value['activity_price']>0){
            $tmp = "<li><img src=".__ROOT__.$value['product_cover']." width='80'><p>".$value['product_name']."&nbsp;&nbsp;&nbsp;x".$value['quantity']."</p><p>".$value['attr_name']."</p><p>￥".$value['activity_price']."</p></li>";
        }else{
            $tmp = "<li><img src=".__ROOT__.$value['product_cover']." width='80'><p>".$value['product_name']."&nbsp;&nbsp;&nbsp;x".$value['quantity']."</p><p>".$value['attr_name']."</p><p>￥".$value['price']."</p></li>";
        }
        
        $str_products = $str_products.$tmp;
      }
      
      return 
      "<ul class='product_order_list'>".$str_products."</ul>";
    }else{
        return;
    }
}

/*product列表页操作*/
function product_order_operation($product_order_id){
    if(!$product_order_id){
        return ;
    }
    $product_order = M('product_order')->find($product_order_id);
    if($product_order['status']=='1'){
        return "<a href='index.php?s=/Admin/ProductOrder/edit/id/".$product_order_id."/model/32'>编辑</a> <a onclick='sendProduct(".$product_order_id.")' style='cursor:pointer;color:#337ab7'>发货</a>";
    }else{
        return "<a href='index.php?s=/Admin/ProductOrder/edit/id/".$product_order_id."/model/32'>编辑</a>";
    }
}
/*product列表页发货*/
function send_product($product_order_id){
    if(!$product_order_id){
        return ;
    }
    $product_order = M('product_order')->find($product_order_id);
    if($product_order['express_num']){
        return $product_order['express_co']."<br/>".$product_order['express_num'];
    }
    if($product_order['status']=='1'){
        return '<input type="text" name="express_num_'.$product_order_id.'" style="display:block;padding:4px 5px;margin-bottom:10px;" placeholder="请填写快递单号"><input type="text" name="express_co_'.$product_order_id.'" style="display:block;padding:4px 5px;margin-bottom:10px;" placeholder="请填写快递公司" value="顺丰到付">';
    }else{
        return '暂无快递单号';
    }
}

/*展览复制网页链接*/
function copy_exhibition_url($exhibition_id){
    if(!$exhibition_id){
        return ;
    }
    return "<a style='cursor:pointer' onclick='copyurl(".$exhibition_id.")'>一键复制</a><input type='text'  value='http://store.sifangartmuseum.org.cn/home/index/exhibition?exhibitionId=".$exhibition_id."' name='url_".$exhibition_id."' style='opacity:0;width:10px;'>";
}

/*产品复制网页链接*/
function copy_product_url($product_id){
    if(!$product_id){
        return ;
    }
    return "<a style='cursor:pointer' onclick='copyurl(".$product_id.")'>一键复制</a><input type='text'  value='http://store.sifangartmuseum.org.cn/home/index/productDetail?productId=".$product_id."' name='url_".$product_id."' style='opacity:0;width:10px;'>";
}

function vip_end_time($visitor_id){
    if(!$visitor_id){
        return ;
    }
    $re_viporder = M('vip_order')
    ->where(array('visitor_id'=>$visitor_id,'status'=>1))
    ->order('end_time desc')
    ->find();
    if(!$re_viporder){
        return ;
    }
    $begin_date = date('Y-m-d',$re_viporder['start_time']);
    $end_date = date('Y-m-d',$re_viporder['end_time']);
    return $begin_date.' 至 '.$end_date;
}

function vip_type($visitor_id){
    if(!$visitor_id){
        return ;
    }
    $re_viporder = M('vip_order')
    ->field('vip_order.*,membership.title as membership_title')
    ->where(array('visitor_id'=>$visitor_id,'vip_order.status'=>1))
    ->join('membership on membership.id = vip_order.vip_type','left')
    ->order('end_time desc')
    ->find();
    return $re_viporder['membership_title'];
}

/*会员种类列表页跳转到会员页*/
function loop_to_visitor($vip_type){
    if(!$vip_type){
        return ;
    }
    return "<a href='index.php?s=/Admin/Visitor/viplists/model/visitor/vip_type/".$vip_type."'>查看</a>";
}